<template>
  <div class="editPassword">
    <el-dialog
      :title="'修改密码'"
      :visible.sync="dialogVisible"
      width="500px"
      @close="closeDialog()"
    >
      <el-form
        ref="passwordData"
        :model="passwordData"
        :rules="rules"
        label-width="100px"
        style="flex:1;margin:0px 30px;"
      >
        <el-form-item label="旧密码：" prop="oldPassword">
          <el-input v-model="passwordData.oldPassword" placeholder="请输入旧密码" type="password" style="width:180px" />
        </el-form-item>
        <el-form-item label="新密码：" prop="newPassword">
          <el-input v-model="passwordData.newPassword" placeholder="请输入新密码" type="password" style="width:180px" @blur="check('newPassword')" />
          <span v-show="newPasswordCheck" class="check">与确认密码不一致</span>
        </el-form-item>
        <el-form-item label="确认密码：" prop="confirmPassword">
          <el-input v-model="passwordData.confirmPassword" placeholder="请重新输入密码" type="password" style="width:180px" @blur="check('confirmPassword')" />
          <span v-show="confirmPasswordCheck" class="check">与新密码不一致</span>

        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="closeDialog()">取消</el-button>
        <el-button type="primary" @click="savePassword">保存</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { changePassword } from '@/api/user'
import md5 from 'js-md5'

export default {
  name: 'EditPassword',
  components: {
  },
  data() {
    return {
      dialogVisible: false,
      newPasswordCheck: false,
      confirmPasswordCheck: false,
      passwordData: {
        oldPassword: '',
        newPassword: '',
        confirmPassword: ''
      },
      rules: {
        oldPassword: [{ required: true, message: '请输入旧密码', trigger: 'blur' }],
        newPassword: [{ required: true, message: '请输入新密码', trigger: 'blur' }],
        confirmPassword: [{ required: true, message: '请输入确认密码', trigger: 'blur' }]
      }
    }
  },
  methods: {
    is() {
      this.dialogVisible = true
    },
    closeDialog() {
      this.dialogVisible = false
      this.$emit('closeEditPassword')
    },
    check(type) {
      switch (type) {
        case 'newPassword':
          if (this.passwordData.newPassword !== this.passwordData.confirmPassword) {
            this.newPasswordCheck = true
            this.confirmPasswordCheck = false
          } else {
            this.confirmPasswordCheck = false
            this.newPasswordCheck = false
          }
          break
        case 'confirmPassword':
          if (this.passwordData.newPassword !== this.passwordData.confirmPassword) {
            this.confirmPasswordCheck = true
            this.newPasswordCheck = false
          } else {
            this.confirmPasswordCheck = false
            this.newPasswordCheck = false
          }
          break
        default:
          break
      }
    },
    savePassword() {
      this.$refs['passwordData'].validate(valid => {
        if (valid) {
          if (this.passwordData.newPassword === this.passwordData.confirmPassword) {
            if (this.passwordData.newPassword !== this.passwordData.oldPassword) {
              const data = {
                oldPassword: md5(this.passwordData.oldPassword),
                newPassword: md5(this.passwordData.newPassword)

              }
              changePassword(data).then(res => {
                this.$notify({
                  title: '成功',
                  message: '修改密码成功',
                  type: 'success'
                })
                this.dialogVisible = false
                this.$emit('closeEditPassword')
              })
            } else {
              this.$message.error('旧密码和新密码相同，请检查')
            }
          } else {
            this.$message.error('你的新密码和确认密码不一致，请检查')
          }
        } else {
          console.log('error submit!!')
          return false
        }
      })
    }
  }
}
</script>

<style lang="less" scoped>
 .editPassword{
   height: 99%;
   div{
    font-size: 14px !important;
    font-family: sans-serif !important;
   }
  div :hover {
    font-size: 14px !important;
    font-family: sans-serif !important;

  }
  .check{
    color: red;
    margin-left: 10px;
    font-size: 12px;
  }
  .check:hover{
    font-size: 12px;
  }
}
</style>
